import {
    getDispatchList,
} from '../../api/message'
import {
    getBindCompanySupplier,
} from '../../../api/app'
import user from '../../../utils/user.js'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        total: 0,
        num: 0,
        sta: "",
        name: "",
        list: [],
        okinsurCompany: [],
        noinsurCompany: [],
        idarr: [],
        alllist: []
    },

    /**
     * 生命周期函数--监听页面加载
     */
    async onLoad(options) {
        let res = await getBindCompanySupplier({
            serveCompanyName: this.data.name,
            companyId: user.companyId
        })
        const eventChannel = this.getOpenerEventChannel()
        const arr = res
        arr.forEach((ele, index) => {
            arr[index].status = false
        })
        eventChannel.on('acceptTransferIds', (data)=> {
            this.setData({
                idarr:data.data
            })
            data.data.forEach(item=>{
                arr.forEach((ele, index) => {
                    if(item === ele.id){
                        arr[index].status = true
                    }
                })
            })
        })
        this.setData({
            total: arr.length,
            sta: options.sta,
            list: arr,
            alllist: arr,
        })
    },
    async iptok(e) {
        console.log(e.detail.value);
        this.setData({
            name: e.detail.value
        })
        let res = await getBindCompanySupplier({
            serveCompanyName: this.data.name,
            companyId: user.companyId
        })
        const arr = res
        arr.forEach((ele, index) => {
            arr[index].status = false
        })
        this.data.idarr.forEach(item => {
            arr.forEach((ite, index) => {
                if (item == ite.id) {
                    arr[index].status = true
                }
            })
        })
        this.setData({
            total: arr.length,
            list: arr,
        })
    },
    async cancelshowipt() {
        this.setData({
            name: ""
        })
        let res = await getBindCompanySupplier({
            serveCompanyName: this.data.name,
            companyId: user.companyId
        })
        const arr = res
        this.data.idarr.forEach(item => {
            arr.forEach((ite, index) => {
                if (item == ite.id) {
                    arr[index].status = true
                }
            })
        })
        console.log(arr);
        this.setData({
            total: arr.length,
            list: arr,
        })
    },
    checkboxChange(e) {
        console.log(e.currentTarget.dataset);
        if (e.currentTarget.dataset.status == false) {
            var i = this.data.list.findIndex((ele, index) => {
                return ele.id == e.currentTarget.dataset.id
            })
            this.data.list[i].status = true
            this.setData({
                list: this.data.list
            })
            this.data.idarr.push(e.currentTarget.dataset.id)
        }
        if (e.currentTarget.dataset.status == true) {
            var listi = this.data.list.findIndex((ele, index) => {
                return ele.id == e.currentTarget.dataset.id
            })
            this.data.list[listi].status = false
            this.setData({
                list: this.data.list
            })
            var i = this.data.idarr.findIndex((ele, index) => {
                return ele == e.currentTarget.dataset.id
            })
            this.data.idarr.splice(i, 1)
        }
        console.log(this.data.idarr);
        this.setData({
            idarr: this.data.idarr
        })
        // console.log(this.data.list);
        if (this.data.sta === 'ok') {
            this.setData({
                okinsurCompany: this.data.idarr,
                num: this.data.idarr.length
            })
        }
        if (this.data.sta === 'no') {
            this.setData({
                noinsurCompany: this.data.idarr,
                num: this.data.idarr.length
            })
        }
    },
    back() {
        const eventChannel = this.getOpenerEventChannel()
        if (this.data.sta === 'no') {
            let arr = []
            this.data.idarr.forEach((ele, index) => {
                this.data.alllist.forEach((item, i) => {
                    if (ele == item.id) {
                        arr.push(item.serveCompanyName)
                    }
                })
            })
            eventChannel.emit('acceptUnitid', {
                data: {
                    sta: "no",
                    no: this.data.noinsurCompany,
                    noarr: arr
                }
            });
        }
        if (this.data.sta === 'ok') {
            let arr = []
            this.data.idarr.forEach((ele, index) => {
                this.data.alllist.forEach((item, i) => {
                    if (ele == item.id) {
                        arr.push(item.serveCompanyName)
                    }
                })
            })
            eventChannel.emit('acceptUnitid', {
                data: {
                    sta: "ok",
                    ok: this.data.okinsurCompany,
                    okarr: arr,
                }
            });
        }
        wx.navigateBack()
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        wx.setStorageSync('noinsurCompany', [])
        wx.setStorageSync('okinsurCompany', [])
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})